/* eslint-disable prettier/prettier */
<!-- prettier-ignore -->
<template>
  <div id="userLoginView">
    <!-- 双栏布局登录页 -->
    <div class="login-container">
      <!-- 左侧欢迎区域 -->
      <div class="welcome-panel">
        <h1>Welcome To AIGC-OJ</h1>
        <p>Don't have an account?</p>
        <a-button class="register-btn" @click="goRegister">Register</a-button>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-panel">
        <h2 class="login-title">Login</h2>
        
        <a-form :model="form" class="login-form" @submit="handleSubmit">
          <a-form-item field="userAccount">
            <a-input
              v-model="form.userAccount"
              placeholder="Username"
              allow-clear
            >
              <template #prefix>
                <icon-user />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item field="userPassword">
            <a-input-password
              v-model="form.userPassword"
              placeholder="Password"
              allow-clear
            >
              <template #prefix>
                <icon-lock />
              </template>
            </a-input-password>
          </a-form-item>

          <div class="forgot-password">
            <a-link>Forgot password?</a-link>
          </div>

          <a-button
            type="primary"
            html-type="submit"
            :loading="loading"
            class="submit-button"
          >
            Login
          </a-button>
          
          <div class="social-login">
            <p>or login with social platforms</p>
            <div class="social-icons">
              <a-button shape="circle" class="social-icon">
                <icon-google />
              </a-button>
              <a-button shape="circle" class="social-icon">
                <icon-facebook />
              </a-button>
              <a-button shape="circle" class="social-icon">
                <icon-github />
              </a-button>
              <a-button shape="circle" class="social-icon">
                <icon-link />
              </a-button>
            </div>
          </div>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import {
  IconUser,
  IconLock,
  IconGoogle,
  IconFacebook,
  IconGithub,
  IconLink,
} from "@arco-design/web-vue/es/icon";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);

const router = useRouter();
const store = useStore();
const loading = ref(false);

/**
 * 提交表单
 * @param data
 */
const handleSubmit = async () => {
  // 表单验证
  if (!form.userAccount) {
    message.warning("请输入账号");
    return;
  }
  if (!form.userPassword) {
    message.warning("请输入密码");
    return;
  }

  loading.value = true;
  try {
    const res = await UserControllerService.userLoginUsingPost(form);
    // 登录成功，跳转到主页
    if (res.code === 0) {
      message.success("登录成功");

      // 清除旧的登录信息
      localStorage.removeItem("loginUser");
      localStorage.removeItem("user");

      // 标记用户已登录（非退出状态）
      await store.dispatch("user/setUserLogin");

      // 重新获取用户信息
      await store.dispatch("user/getLoginUser");

      // 登录成功后跳转
      router.push({
        path: "/home",
        replace: true,
      });
    } else {
      message.error("登录失败：" + res.message);
    }
  } catch (error) {
    message.error("请求出错：" + error);
  } finally {
    loading.value = false;
  }
};

const goRegister = async () => {
  router.push("/user/register");
};
</script>

<style scoped>
#userLoginView {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(120deg, #e0f2fe, #f5f7fa);
  padding: 20px;
}

.login-container {
  display: flex;
  width: 900px;
  height: 580px;
  background-color: white;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

/* 左侧欢迎区域 */
.welcome-panel {
  width: 40%;
  background-color: #7aaaff;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 50px;
  text-align: center;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
}

.welcome-panel h1 {
  font-size: 2.8rem;
  font-weight: 600;
  margin-bottom: 20px;
}

.welcome-panel p {
  margin-bottom: 40px;
  font-size: 1.1rem;
}

.register-btn {
  background-color: transparent;
  border: 2px solid white;
  color: white;
  padding: 5px 20px;
  border-radius: 20px;
  font-weight: 500;
  width: 150px;
}

.register-btn:hover {
  background-color: white;
  color: #7aaaff;
}

/* 右侧登录表单 */
.login-panel {
  width: 60%;
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-title {
  font-size: 2.4rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 40px;
  text-align: center;
}

.login-form {
  width: 100%;
}

:deep(.arco-input-wrapper) {
  border-radius: 5px;
  padding: 10px;
  margin-bottom: 5px;
  background-color: #f5f7fa;
  border: none;
}

:deep(.arco-input) {
  color: #333;
  background-color: #f5f7fa;
}

:deep(.arco-input-prefix) {
  margin-right: 10px;
  color: #999;
}

.forgot-password {
  text-align: right;
  margin: 10px 0 20px;
}

.submit-button {
  width: 100%;
  height: 45px;
  border-radius: 5px;
  font-size: 1rem;
  font-weight: 500;
  margin-top: 10px;
  background-color: #4080ff;
}

.social-login {
  margin-top: 30px;
  text-align: center;
}

.social-login p {
  color: #999;
  margin-bottom: 15px;
  font-size: 0.9rem;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-icon {
  border: 1px solid #ddd;
  background-color: white;
  color: #666;
  font-size: 1.2rem;
}

.social-icon:hover {
  background-color: #f5f7fa;
  color: #4080ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    height: auto;
    width: 100%;
    max-width: 500px;
  }
  .welcome-panel,.login-panel {
    width: 100%;
    border-radius: 0;
  }
  .welcome-panel {
    padding: 30px;
    border-top-right-radius: 20px;
    border-bottom-left-radius: 0;
  }
}
</style>
